package org.spring.springboot.app.community.structure.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.community.info.domain.vo.CommunityInfoSelectVO;
import org.spring.springboot.app.community.structure.domain.po.CommunityStructurePO;
import org.spring.springboot.app.community.structure.domain.vo.*;
import org.spring.springboot.app.community.structure.service.ICommunityStructureService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Pattern;

@Api(description="社区-楼栋、楼层、房间管理操作接口",tags = ApiIndex.CommunityStructureController)
@RequestMapping(value = "/api/communityStructure")
@RestController
public class CommunityStructureController {

    @Autowired
    private ICommunityStructureService communityStructureService;

    @ApiOperation(value = "快速创建楼栋")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/fastSaveBuild", method = RequestMethod.POST)
    public R fastSaveBuild(
            @ApiParam(value = "快速创建小区对象", required = true) @RequestBody CommunityStructureFastVO vo
    ) {
        R result = new R();
        if(vo==null){
            result.setCode(1);
            result.setMessage("对象不能为空");
            return result;
        }
        if(vo.getBuildId()==null ||vo.getBuildId().length()<1){
            result.setCode(1);
            result.setMessage("大楼ID不能为空");
            return result;
        }
        if(vo.getFloorNum()==null ||vo.getFloorNum()>200 || vo.getFloorNum()<1){
            result.setCode(1);
            result.setMessage("楼层数不能为空，楼层范围在1-200层");
            return result;
        }
        if(vo.getHouseNum()==null ||vo.getHouseNum()>30 || vo.getFloorNum()<1){
            result.setCode(1);
            result.setMessage("房间数不能为空，房间数范围在1-30层");
            return result;
        }
        //快速创建
        communityStructureService.fastSaveBuild(vo);
        return  result;
    }
    @ApiOperation(value = "传入任意ID查询父级ID")
    @RequestMapping(value = "/find_parent_id", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<String> findParentId(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) {
        R<String> result = new R<String>();
        String parentIds = communityStructureService.findByParent(id);
        result.setData(parentIds);
        return result;
    }
    @ApiOperation(value = "通过ID查询楼栋信息")
    @RequestMapping(value = "/find_build_by_id", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<CommunityStructurePO> findBuildById(
            @ApiParam(value = "楼栋ID", required = true) @RequestParam String buildId
    ) {
        R<CommunityStructurePO> result = new R<CommunityStructurePO>();
        CommunityStructurePO vo = communityStructureService.findById(buildId);
        result.setData(vo);
        return result;
    }
    @ApiOperation(value = "通过ID查询楼层信息")
    @RequestMapping(value = "/find_floor_by_id", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<CommunityStructurePO> findFloordById(
            @ApiParam(value = "楼层ID", required = true) @RequestParam String floorId
    ) {
        R<CommunityStructurePO> result = new R<CommunityStructurePO>();
        CommunityStructurePO vo = communityStructureService.findById(floorId);
        result.setData(vo);
        return result;
    }
    @ApiOperation(value = "通过ID查询房间信息")
    @RequestMapping(value = "/find_house_by_id", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<CommunityStructureHouseVO> findHouseById(
            @ApiParam(value = "房间ID", required = true) @RequestParam String houseId
    ) {
        R<CommunityStructureHouseVO> result = new R<CommunityStructureHouseVO>();
        CommunityStructureHouseVO vo = communityStructureService.findHouseById(houseId);
        result.setData(vo);
        return result;
    }
    @ApiOperation(value = "根据父级ID查询子集最大待添加序列Sort")
    @RequestMapping(value = "/find_max_sort", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<Integer> findBuildMaxSort(
            @ApiParam(value = "类型Type（2.查楼栋 3.查楼层 4.查房间）", required = true) @RequestParam String type,
            @ApiParam(value = "父类id(type选2传小区ID,选3传楼栋ID。以此类推)", required = true) @RequestParam String parentId
    ) {
        R<Integer> result = new R<Integer>();
        if(!type.equals("2") && !type.equals("3") && !type.equals("4")){
            result.setCode(1);
            result.setMessage("type只允许输入 2.查楼栋 3.查楼层 4.查房间");
            return result;
        }
        Integer max = communityStructureService.findMaxSort(parentId,type);
        if(max==null){
            max=0;
        }
        result.setData(max);
        return result;
    }
    @ApiOperation(value = "通过小区ID查询楼栋列表")
    @RequestMapping(value = "/find_build_by_info_id", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<CommunityStructurePO>> findBuildByInfoId(
            @ApiParam(value = "小区ID", required = true) @RequestParam String infoId
    ) {
        R<List<CommunityStructurePO>> result = new R<List<CommunityStructurePO>>();
        List<CommunityStructurePO> list = communityStructureService.findByParentId(infoId);
        result.setData(list);
        return result;
    }
    @ApiOperation(value = "通过楼栋ID查询楼层列表")
    @RequestMapping(value = "/find_floor_by_build_id", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<CommunityStructurePO>> findFloorByBuildId(
            @ApiParam(value = "楼栋ID", required = true) @RequestParam String buildId
    ) {
        R<List<CommunityStructurePO>> result = new R<List<CommunityStructurePO>>();
        List<CommunityStructurePO> list = communityStructureService.findByParentId(buildId);
        result.setData(list);
        return result;
    }
    @ApiOperation(value = "通过楼层ID查询房间列表")
    @RequestMapping(value = "/find_house_by_floor_id", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<CommunityStructureHouseVO>> findHouseByFloorId(
            @ApiParam(value = "楼层ID", required = true) @RequestParam String floorId
    ) {
        R<List<CommunityStructureHouseVO>> result = new R<List<CommunityStructureHouseVO>>();
        List<CommunityStructureHouseVO> list = communityStructureService.findHouseByFloorId(floorId);
        result.setData(list);
        return result;
    }
    @ApiOperation(value = "保存楼栋")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/saveBuild", method = RequestMethod.POST)
    public R saveBuild(
            @ApiParam(value = "保存楼栋对象", required = true) @RequestBody CommunityStructureBuildVO vo
    ) {
        R result = new R();
        if(vo.getInfoId()==null || vo.getInfoId().equals("")){
            result.setCode(1);
            result.setMessage("小区ID不能为空");
            return result;
        }
        if(vo.getSort()==null || vo.getSort().equals("")){
            result.setCode(1);
            result.setMessage("排序");
            return result;
        }
        //验证楼栋数是否存在
        int count=communityStructureService.queryByIdAndType(vo.getInfoId(),vo.getSort(),"2");
        if(count>0){
            result.setCode(1);
            result.setMessage("楼栋序列"+vo.getSort()+"已经存在，请重新输入");
            return result;
        }
        //保存楼栋
        communityStructureService.saveBuild(vo);
        return  result;
    }
    @ApiOperation(value = "保存楼层")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/saveFloor", method = RequestMethod.POST)
    public R saveFloor(
            @ApiParam(value = "保存楼层对象", required = true) @RequestBody CommunityStructureFloorVO vo
    ) {
        R result = new R();
        if(vo.getBuildId()==null || vo.getBuildId().equals("")){
            result.setCode(1);
            result.setMessage("楼层ID不能为空");
            return result;
        }
        if(vo.getSort()==null || vo.getSort().equals("")){
            result.setCode(1);
            result.setMessage("排序");
            return result;
        }
        //验证楼层数是否存在
        int count=communityStructureService.queryByIdAndType(vo.getBuildId(),vo.getSort(),"3");
        if(count>0){
            result.setCode(1);
            result.setMessage("楼层序列"+vo.getSort()+"已经存在，请重新输入");
            return result;
        }
        //保存楼栋;
        communityStructureService.saveFloor(vo);
        return  result;
    }
    @ApiOperation(value = "保存房间")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/saveHouse", method = RequestMethod.POST)
    public R saveHouse(
            @ApiParam(value = "保存房间对象", required = true) @RequestBody CommunityStructureHouseVO vo

    ) {
        R result = new R();
        if(vo.getFloorId()==null || vo.getFloorId().equals("")){
            result.setCode(1);
            result.setMessage("所属楼层ID不能为空");
            return result;
        }
        if(vo.getName()==null || vo.getName().equals("")){
            result.setCode(1);
            result.setMessage("房间名称不能为空");
            return result;
        }
        if(vo.getSort()==null || vo.getSort().equals("")){
            result.setCode(1);
            result.setMessage("房间排序不能为空");
            return result;
        }
        //验证楼层数是否存在
        int count=communityStructureService.queryByIdAndType(vo.getFloorId(),vo.getSort().toString(),"4");
        if(count>0){
            result.setCode(1);
            result.setMessage("房间序列"+vo.getSort()+"已经存在，请重新输入");
            return result;
        }
        //保存房间
        communityStructureService.saveHouse(vo);
        return  result;
    }
    @ApiOperation(value = "编辑楼栋")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/updateBuild", method = RequestMethod.PUT)
    public R updateBuild(
            @ApiParam(value = "编辑楼栋对象", required = true) @RequestBody CommunityStructureBuildUpVO vo
    ) {
        R result = new R();
        if(vo.getId()==null || vo.getId().equals("")){
            result.setCode(1);
            result.setMessage("楼栋ID不能为空");
            return result;
        }
        if(vo.getName()==null || vo.getName().equals("")){
            result.setCode(1);
            result.setMessage("楼栋名称不能为空");
            return result;
        }
        if(vo.getSort()==null || vo.getSort().equals("")){
            result.setCode(1);
            result.setMessage("楼栋排序不能为空");
            return result;
        }
        //验证楼层sort是否存在
        CommunityStructurePO build=communityStructureService.findById(vo.getId());
        int count=communityStructureService.queryByIdAndType(build.getParentId(),vo.getSort(),"2");
        if(build.getSort().toString().equals(vo.getSort())){
            //修改楼栋
            CommunityStructurePO po=new CommunityStructurePO();
            po.setId(vo.getId());
            po.setSort(Byte.valueOf(vo.getSort()));
            po.setName(vo.getName());
            communityStructureService.updateBuild(po);
        }else{
            if(count > 0){
                result.setCode(1);
                result.setMessage("楼栋序列"+vo.getSort()+"已经存在，请重新输入");
                return result;
            }else{
                    //修改楼栋
                    CommunityStructurePO po=new CommunityStructurePO();
                    po.setId(vo.getId());
                    po.setSort(Byte.valueOf(vo.getSort()));
                    po.setName(vo.getName());
                    communityStructureService.updateBuild(po);
            }
        }

        return  result;
    }
    @ApiOperation(value = "编辑楼层")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/updateFloor", method = RequestMethod.PUT)
    public R updateFloor(
            @ApiParam(value = "编辑楼层对象", required = true) @RequestBody CommunityStructureFloorUpVO vo
    ) {
        R result = new R();
        if(vo.getId()==null || vo.getId().equals("")){
            result.setCode(1);
            result.setMessage("楼层ID不能为空");
            return result;
        }
        if(vo.getName()==null || vo.getName().equals("")){
            result.setCode(1);
            result.setMessage("楼层名称不能为空");
            return result;
        }
        if(vo.getSort()==null || vo.getSort().equals("")){
            result.setCode(1);
            result.setMessage("楼层排序不能为空");
            return result;
        }
        //验证楼层sort是否存在
        CommunityStructurePO floor=communityStructureService.findById(vo.getId());
        int count=communityStructureService.queryByIdAndType(floor.getParentId(),vo.getSort().toString(),"3");
        if(floor.getSort().toString().equals(vo.getSort())){
            //修改楼栋
            CommunityStructurePO po=new CommunityStructurePO();
            po.setId(vo.getId());
            po.setSort(Byte.valueOf(vo.getSort()));
            po.setName(vo.getName());
            communityStructureService.updateFloor(po);
        }else{
            if(count > 0){
                result.setCode(1);
                result.setMessage("楼层序列"+vo.getSort()+"已经存在，请重新输入");
                return result;
            }else{
                //修改楼栋
                CommunityStructurePO po=new CommunityStructurePO();
                po.setId(vo.getId());
                po.setSort(Byte.valueOf(vo.getSort()));
                po.setName(vo.getName());
                communityStructureService.updateFloor(po);
            }
        }

        return  result;
    }
    @ApiOperation(value = "编辑房间")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/updateHouse", method = RequestMethod.PUT)
    public R updateHouse(
            @ApiParam(value = "编辑房间对象", required = true) @RequestBody CommunityStructureHouseUpVO vo
    ) {
        R result = new R();
        if(vo.getId()==null || vo.getId().equals("")){
            result.setCode(1);
            result.setMessage("房间ID不能为空");
            return result;
        }
        if(vo.getName()==null || vo.getName().equals("")){
            result.setCode(1);
            result.setMessage("房间名称不能为空");
            return result;
        }
        if(vo.getSort()==null || vo.getSort().equals("")){
            result.setCode(1);
            result.setMessage("房间排序不能为空");
            return result;
        }
        boolean money = Pattern.matches("([1-9]\\d{0,9}|0)([.]?|(\\.\\d{1,2})?)", vo.getMonthCost());
        if(vo.getMonthCost()!=null && !vo.getMonthCost().toString().equals("")){
            if(!money){
                result.setCode(1);
                result.setMessage("每月物管费格式不正确");
                return result;
            }
        }
        if(vo.getState()==null && !vo.getState().equals("1") && !vo.getState().equals("0")){
            result.setCode(1);
            result.setMessage("房间状态还能为1.启用 0.禁用，不能为空");
            return result;
        }
        //验证楼层sort是否存在
        CommunityStructurePO house=communityStructureService.findById(vo.getId());
        int count=communityStructureService.queryByIdAndType(house.getParentId(),vo.getSort().toString(),"4");
        if(house.getSort().toString().equals(vo.getSort())){
            communityStructureService.updateHouse(vo);
        }else{
            if(count > 0){
                result.setCode(1);
                result.setMessage("房间序列"+vo.getSort()+"已经存在，请重新输入");
                return result;
            }else{
                communityStructureService.updateHouse(vo);
            }
        }

        return  result;
    }
    @ApiOperation(value = "删除楼栋")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/deleteBuild", method = RequestMethod.DELETE)
    public R deleteBuild(
            @ApiParam(value = "楼栋ID", required = true) @RequestParam String buildId

    ) {
        R result = new R();
        if(buildId==null || buildId.equals("")){
            result.setCode(1);
            result.setMessage("楼栋ID不能为空");
            return result;
        }
        //验证楼层sort是否存在
        int ret=communityStructureService.deleteByParentIds(buildId);
        if(ret<1){
            result.setCode(1);
            result.setMessage("删除楼栋失败");
            return result;
        }
        return  result;
    }
    @ApiOperation(value = "删除楼层")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/deleteFloor", method = RequestMethod.DELETE)
    public R deleteFloor(
            @ApiParam(value = "楼层ID", required = true) @RequestParam String floorId

    ) {
        R result = new R();
        if(floorId==null || floorId.equals("")){
            result.setCode(1);
            result.setMessage("楼层ID不能为空");
            return result;
        }
        //验证楼层sort是否存在
        int ret=communityStructureService.deleteByParentIds(floorId);
        if(ret<1){
            result.setCode(1);
            result.setMessage("删除楼层失败");
            return result;
        }
        return  result;
    }
    @ApiOperation(value = "删除房间")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/deleteHouse", method = RequestMethod.DELETE)
    public R deleteHouse(
            @ApiParam(value = "房间ID", required = true) @RequestParam String houseId

    ) {
        R result = new R();
        if(houseId==null || houseId.equals("")){
            result.setCode(1);
            result.setMessage("房间ID不能为空");
            return result;
        }
        //验证楼层sort是否存在
        int ret=communityStructureService.deleteByParentIds(houseId);
        if(ret<1){
            result.setCode(1);
            result.setMessage("删除房间失败");
            return result;
        }
        return  result;
    }
}
